//
//  WBWelcomeView.swift
//  Weibo
//
//  Created by Albert Zhu on 16/10/17.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeView: UIView {
    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var widthConstraint: NSLayoutConstraint!
    
    class func welcomeView() -> WBWelcomeView {
        
        let nib = UINib.init(nibName: "WBWelcomeView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0]
        
        return v as! WBWelcomeView
    }
    
    override func awakeFromNib() {
        
        guard let urlString = WBNetworkManager.shared.userAccount.avatar_large,
            let url = URL(string: urlString) else {return}
        
        iconView.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "avatar_default"))
        iconView.layer.cornerRadius = widthConstraint.constant * 0.5
        iconView.layer.masksToBounds = true
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        layoutIfNeeded()
        
        bottomConstraint.constant = bounds.height - 200
        
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
            self.layoutIfNeeded()
            }, completion: { _ in
                UIView.animate(withDuration: 1.0, animations: {
                    self.tipLabel.alpha = 1.0
                    }, completion: { _ in
                        self.removeFromSuperview()
                })
        })
    }
}
